import {Controller, Get, Body, Request, Query, HttpService, Post, Req, Inject, Param, UseInterceptors, Logger} from '@nestjs/common';
import { AppService } from './app.service';
import { SignatureDTO } from './model/common';
import { WxhttpService } from './module/wxhttp/wxhttp.service';
import { ResTokenDto, WxUserInfoDto } from './module/wxhttp/model/common';
import { ApiTags, ApiResponse } from '@nestjs/swagger';
// import { Transport } from './common/define';

import { MessagePattern, Client, ClientProxy, Transport, ClientProxyFactory, Payload, Ctx, RedisContext} from '@nestjs/microservices';
import {LoggingInterceptor, WxLogger} from './middleware/logger.interceptor';
import { WxLoggerService } from './services/logger.service';

@ApiTags('home')
@Controller('home')
@UseInterceptors(new LoggingInterceptor())
export class AppController {

  constructor(
    private readonly appService: AppService,
    private readonly httpService: WxhttpService,
    @Inject('WX_SERVICE') private readonly client: ClientProxy,
  ) { }

  @MessagePattern('notifications')
  getNotifications(@Payload() data: number[], @Ctx() context: RedisContext) {
    Logger.log(context.getChannel() + JSON.stringify(data), 'Redis-Sub-Noti');
  }

  // @Get('getLogList')
  // getLogList() {
  //   return this.appService.getLogList();
  // }

  @Get('/testApi')
  async getTestInfo() {
    const value = await WxLogger.find().limit(10);
    return  value;
  }

  @Get('/postRedis')
  async postRedis(@Query() query) {
    const obser = this.client.emit('redisChat', query);
    return await obser.toPromise();
  }

  @Get('/index')
  getHello(): string {
    return this.appService.getHello();
  }

  // 校验token接口
  @Get('/handleMessage')
  handleMessageValidToken(@Query() query: SignatureDTO): string {
    return this.appService.checkSignature(query);
  }

  // 接收微信post的消息
  @Post('/handleMessage')
  handleMessage(@Body() request): string {
    console.log(request);
    return '';
  }

  // 获取token
  @Get('/getToken')
  async getWxToken(): Promise<string>  {
    const resToken = await this.httpService.getWxTokenString();
    return resToken;
  }

  // 检测网络
  @Get('/checkWxNet')
  async checkWxNet() {
    const res = await this.httpService.checkWxNetwork();
    return res;
  }
  // 获取微信 公众号 授权回调
  @Get('/wxRedirect')
  async wxRedirect(@Query() query: { code: string, state: string }) {

     const authData = await this.httpService.getWxAuthRedirect(query);
     const userInfo = await this.httpService.getWxUserInfoWithToken(authData.access_token, authData.openid);
  }

  // 生成js-sdk的sign签名
  @Get('/getJsSignature')
  getJsSignature(@Req() req) {

    const url = 'https://abouti.top/web/wx/share/';
    const sign =  this.httpService.getJsApiSign(url);
    return sign;
  }

  @Get('/getUserInfo/:id')
  getUserInfoWithOpenId(@Param() param) {

    const sign = this.httpService.getUserInfoWithOpenID(param.id);
    return sign;

  }

  // 获取微信个人信息
  @Get('/wxUserInfo')
  @ApiResponse({ type: WxUserInfoDto })
  async getWxUserInfo(): Promise<WxUserInfoDto> {
    //  如果获取不到 授权
    return this.httpService.getWxUserInfoWithCache();
  }
  // 创建二维码
  @Get('/getQRCode')
  async getQRCode() {
    const res = await this.httpService.getQRCode(1234);
    return res;
  }

}
